package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://bbs.shangdu.com/ 商都社区
 * @author Persh
 */
public class ShangduBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://bbs.shangdu.com/t/[0-9]{8}/[0-9]*/[0-9]*-1.htm",
				//http://bbs.shangdu.com/t/20150611/170010019598/9598-1.htm
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String authorUrl = null;
		String author = null;

		Document doc = Jsoup.parse(htmltxt);
		title =doc.select("title").text();
		title = StringUtils.substringBefore(title, "_");
		if(StringUtils.isBlank(title)){
		}

		Element firstDiv = doc.select("div#PageBody > div[class=z_tie]").first();
		if(firstDiv!=null){
			content = HtmlCleaner.getContentHtml(url, firstDiv.select("div.tit"));
			String tempdate=firstDiv.select("div.time").text();
			date = DateUtils.matchDate(tempdate);
			authorUrl = firstDiv.select("div.user").html();
			authorUrl = StringUtils.substringBetween(authorUrl, "\"", "\"");
			if(authorUrl!=null){
				UrlMeta authorMeta = CrawlHTML.responseToURL(authorUrl);
				String authorHtml = authorMeta.getHtml();
				if(authorHtml!=null){
					Document parse = Jsoup.parse(authorHtml);
					author = parse.select("span.name").text();
				}
			}
		}
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setAuthor(author);

		/**
		 * 更新路径
		 */
		String updateUrl = null;
		try {
			String lastUrl = url.substring(url.lastIndexOf("/")+1);
			String firstUrl = url.substring(0, url.lastIndexOf("/")+1);
			String autoid = lastUrl.substring(0, lastUrl.indexOf("-"));
			String ids = lastUrl.substring(lastUrl.indexOf("-")+1, lastUrl.indexOf("."));
			updateUrl = firstUrl + "js.js?autoid="+autoid+"&ids="+ids;
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(updateUrl!=null){
			bbs.setUpdateUrl(updateUrl);
			NewsMeta update = Update(bbs);
			return update;
		}
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String htmltxt = responseToURL.getHtml();
					Integer clickNum=0;
					Integer commentNum=0;
					try {
						if(htmltxt.contains("var maxnum=")){
							htmltxt = htmltxt.substring(htmltxt.indexOf("var maxnum="));
							String lastPage = htmltxt.substring(htmltxt.indexOf("=")+1,htmltxt.indexOf(";"));
							if(!lastPage.equals("1")){
								commentNum = Integer.parseInt(lastPage) *20;
							}else{
								String url = meta.getUrl();
								responseToURL = CrawlHTML.responseToURL(url);
								htmltxt = responseToURL.getHtml();
								Document doc = Jsoup.parse(htmltxt);
								commentNum = doc.select("div.z_tie").size();
							}
						}
					} catch (Exception e) {
					}
					meta.setClickNum(clickNum);
					meta.setCommentNum(commentNum);
					return meta;
				}
			}
		}
		return null;
	}
    
	
}
